package cn.tianyigps.tianyigpsmiletask.assign;

import cn.tianyigps.tianyigpskafka.task.bean.Task;
import cn.tianyigps.tianyigpskafka.task.manger.TaskManger;
import cn.tianyigps.tianyigpsmiletask.bean.EquipmentDto;
import cn.tianyigps.tianyigpsmiletask.dao.EquipmentDao;
import cn.tianyigps.tianyigpspublic.bean.EquipmentModel;
import cn.tianyigps.tianyigpspublic.util.FormatUtil;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

@Component
@Slf4j
public class CalMile {

    @Autowired
    EquipmentDao equipmentDao;
    @Autowired
    TaskManger taskManger;

    public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
    }


    public void calDistance(Task task) {

        Map<String, Object> eqMaps = task.getParams();
        ObjectMapper objectMapper = new ObjectMapper();
        List<EquipmentDto> equipmentDto = new ArrayList<>();
        try {
            //序列化得到List<EquipmentDto>
            String json = objectMapper.writeValueAsString(eqMaps.get("equipmentIds"));
            JavaType javaType = getCollectionType(ArrayList.class, EquipmentDto.class);
            equipmentDto = objectMapper.readValue(json, javaType);
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<Map<String, String>> distanceList = new ArrayList<>();

        try {
            if (!CollectionUtils.isEmpty(equipmentDto)) {
                //循环查询在时间范围内的eqipment
                for (EquipmentDto eq : equipmentDto) {
                    if (!StringUtils.isEmpty(eq.getEquipmentId()) && !StringUtils.isEmpty(eq.getStartTime())
                            && !StringUtils.isEmpty(eq.getEndTime())) {
                        Date start = FormatUtil.format2Date(eq.getStartTime());
                        Date end = FormatUtil.format2Date(eq.getEndTime());

                        Map<String, String> distanceMap = new HashMap<>();

                        List<EquipmentModel> equipmentCheckModel = equipmentDao.queryEqCMByList(eq.getEquipmentId(), start, end);
                        //只有当查询的点大于一个时才有长度
                        if (!CollectionUtils.isEmpty(equipmentCheckModel) && equipmentCheckModel.size() > 1) {
                            double countmile = 0.0;
                            for (int i = 1; i < equipmentCheckModel.size(); i++) {
                                //上一条的经纬度
                                double lng1 = equipmentCheckModel.get(i - 1).getGpsLongitude();
                                double lat1 = equipmentCheckModel.get(i - 1).getGpsLatitude();
                                //本条的经纬度
                                double lng2 = equipmentCheckModel.get(i).getGpsLongitude();
                                double lat2 = equipmentCheckModel.get(i).getGpsLatitude();
                                double mile = MileageCalculation.calcDistance(lng1, lat1, lng2, lat2);
                                countmile += mile;
                            }
                            distanceMap.put("equipmentId", equipmentCheckModel.get(0).getEquipmentId());
                            distanceMap.put("distance", String.valueOf(countmile));
                            distanceList.add(distanceMap);
                        }
                    }
                }
            }
            Task nextTask = task.getNextTask();
            Map<String, Object> params = nextTask.getParams();
            params.put("distance", distanceList);
            nextTask.setParams(params);
            nextTask.setId(task.getId());
            taskManger.distributeTask(nextTask);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
